<template>
  <div>
    <el-form :inline="true" :model="query" class="demo-form-inline">
      <el-form-item label="菜单名称">
        <el-input v-model="query.menuName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :loading="queryLoading" @click="getTree">查询</el-button>
        <el-button @click="reset">重置</el-button>
        <el-button v-if="$hasApiPerm('menu:add')" type="success" @click="showAdd()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="treeData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      default-expand-all
      :header-cell-style="{textAlign : 'center'}"
      :cell-style="{textAlign : 'center'}"
      :tree-props="{children: 'children'}"
    >
      <el-table-column
        prop="menuType"
        label="菜单类型"
      >
        <template slot-scope="{row}">
          <el-tag v-if="row.menuType === '1'">目录</el-tag>
          <el-tag v-if="row.menuType === '2'" type="success">菜单</el-tag>
          <el-tag v-if="row.menuType === '3'" type="warning">按钮</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="icon"
        label="菜单图标"
      >
      </el-table-column>
      <el-table-column
        prop="menuName"
        label="菜单名称"
      >
      </el-table-column>
      <el-table-column
        prop="sort"
        label="排序"
      >
      </el-table-column>
      <el-table-column
        prop="routePath"
        label="路由地址"
      >
      </el-table-column>
      <el-table-column
        prop="componentPath"
        label="组件地址"
        width="250"
      >
      </el-table-column>
      <el-table-column
        prop="permission"
        label="权限标识"
      >
      </el-table-column>
      <el-table-column
        prop="available"
        label="是否启用"
      >
        <template slot-scope="{row}">
          <el-switch
            v-model="row.available"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0"
            @change="statusChange(row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        width="160"
      >
        <template slot-scope="{row}">
          {{ resolveTime(row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="200"
      >
        <template slot-scope="{row}">
          <el-button v-if="$hasApiPerm('menu:get')" type="text" size="small" @click="showView(row.id)">查看</el-button>
          <el-button v-if="$hasApiPerm('menu:add')" type="text" size="small" @click="showAdd(row.id)">新增</el-button>
          <el-button v-if="$hasApiPerm('menu:update')" type="text" size="small" @click="showUpdate(row.id)">编辑</el-button>
          <el-button v-if="$hasApiPerm('menu:del')" type="text" size="small" @click="ensureDelete(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <opt-menu :dialog-visible.sync="optDialog" :opt-flag="optFlag" :pid="pid" :menu-id="menuId"
              @refresh="getTree"
    />
  </div>
</template>

<script>
import menuApi from '@/api/system/menuApi'
import OptMenu from '@/views/system/menu/components/OptMenu.vue'
import moment from 'moment'

export default {
  name: 'MenuList',
  components: { OptMenu },
  data() {
    return {
      query: {},
      queryLoading: false,
      treeData: [],
      optDialog: false,
      optFlag: '',
      pid: null,
      menuId: null
    }
  },
  created() {
    this.getTree()
  },
  methods: {
    getTree() {
      this.queryLoading = true
      menuApi.getTree(this.query).then(res => {
        this.treeData = res.data
      }).finally(() => {
        this.queryLoading = false
      })
    },
    resolveTime(time) {
      return moment(time).format('YYYY-MM-DD HH:mm:ss')
    },
    statusChange(row) {
      menuApi.status(row.id, row.available).then(res => {
        this.$message({
          message: '操作成功！',
          type: 'success'
        })
        this.getPage()
      })
    },
    showView(menuId) {
      this.optDialog = true
      this.optFlag = 'view'
      this.pid = null
      this.menuId = menuId
    },
    showAdd(pid = null) {
      this.optDialog = true
      this.optFlag = 'add'
      this.pid = pid
      this.menuId = null
    },
    showUpdate(menuId) {
      this.optDialog = true
      this.optFlag = 'update'
      this.pid = null
      this.menuId = menuId
    },
    ensureDelete(menuId) {
      this.$confirm('确定执行删除操作吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteOpt(menuId)
      }).catch(() => {
      })
    },
    deleteOpt(menuId) {
      menuApi.delete(menuId).then(res => {
        this.$message({
          message: '操作成功！',
          type: 'success'
        })
        this.getTree()
      })
    },
    reset() {
      this.query = {}
    }
  }
}
</script>

<style scoped>

</style>
